﻿package com.PlantVsZombies.db.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.PlantVsZombies.db.model.RankList;
import com.PlantVsZombies.util.DBUtil;

public class RankListDao {
	QueryRunner qr = new QueryRunner(DBUtil.getDataSource());

	public boolean insertRankList(RankList rankList ) {
		String sql = "insert into t_rank_list(player, score, pattern, time) values(?,?,?,?)";
		Object[] params = { rankList.getPlayer(), rankList.getScore(), rankList.getPattern(), rankList.getPattern(), new Date() };
		try {
			return qr.update(sql, params) > 0;
		} catch (SQLException e) {
			System.out.println("插入排行榜信息失败");
			return false;
		}
	}

	
	public List<RankList> selectAll() {
		String sql = "select * from t_rank_list order by score";
		try {
			return (List<RankList>) qr.query(sql, new BeanListHandler(RankList.class));
		} catch (SQLException e) {
			System.out.println("查询排行榜出错");
			return new ArrayList<>();
		}
	}
}
